package cn.xiaoxiang.pet.service.impl;

import cn.xiaoxiang.basic.jwt.UserInfo;
import cn.xiaoxiang.basic.mapper.BaseMapper;
import cn.xiaoxiang.basic.util.LoginContext;
import cn.xiaoxiang.org.domain.Employee;
import cn.xiaoxiang.org.domain.Shop;
import cn.xiaoxiang.org.domain.Type;
import cn.xiaoxiang.org.mapper.EmployeeMapper;
import cn.xiaoxiang.org.mapper.ShopMapper;
import cn.xiaoxiang.pet.domain.PetType;
import cn.xiaoxiang.pet.mapper.PetTypeMapper;
import cn.xiaoxiang.pet.service.IPetTypeService;
import cn.xiaoxiang.basic.service.impl.BaseServiceImpl;
import cn.xiaoxiang.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxiang
 * @since 2022-12-03
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {


    @Override
    public List<PetType> tree() {
      List<PetType> result = new ArrayList<>();
        List<PetType> allType = super.findAll();
        Map<Long, PetType> typeMap = allType.stream().collect(Collectors.toMap(PetType::getId, e -> e));
        allType.forEach(Type->{
            if(null==Type.getPid()){
               result.add(Type);
            }else {
                PetType petType = typeMap.get(Type.getPid());
                petType.getChildren().add(Type);

            }
        });
        return result;
    }
}
